package com.tianbing.adlottery.controller.storeadmin;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import com.jubao.ejb.business.ProductBusiness;
import com.jubao.ejb.business.StoreBusiness;
import com.jubao.ejb.dao.CouponDao;
import com.jubao.ejb.entity.Coupon;
import com.jubao.ejb.entity.CouponItem;
import com.jubao.ejb.entity.ProductPrice;
import com.jubao.ejb.entity.StoreTransaction;
import com.jubao.ejb.entity.User;
import com.tianbing.adlottery.helper.SkinUtils;
import com.tianbing.adlottery.util.UserSession;

/**
 * MultiActionController for admin my store.
 * 
 * @author Conan Chen
 * @since 2007.12.15
 */
public class AdminStoreController extends MultiActionController {

	Logger logger = Logger.getLogger(AdminStoreController.class.getName());

	private String productPriceListView;

	private String couponListView;

	private String setStoreServantView;

	private String couponItemListView;

	private String viewStoreServantDetailView;

	private String storeTransactionListView;
	private ProductBusiness productBusiness;

	private CouponDao couponDao;

	private StoreBusiness storeBusiness;

	private SkinUtils skinUtils;

	public ModelAndView listProductPrice(HttpServletRequest request, HttpServletResponse response) throws Exception {

		ModelAndView mv = new ModelAndView(productPriceListView);
		Map<String, Object> model = mv.getModel();

		UserSession auth = skinUtils.getUserAuthorization(request, response);
		long storeSysId = auth.getManagedStoreSysId();
		// get site2content for this store
		Integer pgno = ServletRequestUtils.getIntParameter(request, "pgno", 0);
		Integer pgsize = ServletRequestUtils.getIntParameter(request, "pgsize", 30);

		List<ProductPrice> productPriceList = productBusiness.getProductPriceList(-1, storeSysId, -1, pgno, pgsize);
		long totalCount = productBusiness.getProductPriceCount(-1, storeSysId, -1);
		model.put("productPriceList", productPriceList);

		int pageItemNum = productPriceList == null ? 0 : productPriceList.size();
		String otherParam = null;
		model = skinUtils
				.preparePageFooterParameters(request, pageItemNum, totalCount, pgno, pgsize, otherParam, model);

		return mv;
	}

	public ModelAndView setStoreServant(HttpServletRequest request, HttpServletResponse response) throws Exception {

		ModelAndView mv = new ModelAndView(setStoreServantView);
		Map<String, Object> model = mv.getModel();

		long usersid = ServletRequestUtils.getLongParameter(request, "usersid", -1);
		UserSession auth = skinUtils.getUserAuthorization(request, response);
		long storeSysId = auth.getManagedStoreSysId();

		storeBusiness.setServant(storeSysId, usersid);

		return mv;
	}

	public ModelAndView viewStoreServant(HttpServletRequest request, HttpServletResponse response) throws Exception {

		ModelAndView mv = new ModelAndView(viewStoreServantDetailView);
		Map<String, Object> model = mv.getModel();

		UserSession auth = skinUtils.getUserAuthorization(request, response);
		long storeSysId = auth.getManagedStoreSysId();

		User user = storeBusiness.findServant(storeSysId);
		model.put("user", user);
		return mv;
	}

	public ModelAndView listCoupon(HttpServletRequest request, HttpServletResponse response) throws Exception {

		ModelAndView mv = new ModelAndView(couponListView);
		Map<String, Object> model = mv.getModel();

		UserSession auth = skinUtils.getUserAuthorization(request, response);
		// get site2content for this store
		Integer pgno = ServletRequestUtils.getIntParameter(request, "pgno", 0);
		Integer pgsize = ServletRequestUtils.getIntParameter(request, "pgsize", 30);

		long storeSysId = auth.getManagedStoreSysId();

		List<Coupon> couponList = storeBusiness.getCouponList(storeSysId, pgno, pgsize);
		long totalCount = storeBusiness.getCouponCount(storeSysId);

		model.put("couponList", couponList);

		int pageItemNum = couponList == null ? 0 : couponList.size();
		String otherParam = null;
		model = skinUtils
				.preparePageFooterParameters(request, pageItemNum, totalCount, pgno, pgsize, otherParam, model);

		return mv;
	}

	public ModelAndView listCouponItem(HttpServletRequest request, HttpServletResponse response) throws Exception {

		ModelAndView mv = new ModelAndView(couponItemListView);
		Map<String, Object> model = mv.getModel();

		UserSession auth = skinUtils.getUserAuthorization(request, response);
		// get site2content for this store
		Integer pgno = ServletRequestUtils.getIntParameter(request, "pgno", 0);
		Integer pgsize = ServletRequestUtils.getIntParameter(request, "pgsize", 30);

		long storeSysId = auth.getManagedStoreSysId();
		long couponSysId = ServletRequestUtils.getLongParameter(request, "cpsid", -1);

		Coupon coupon = couponDao.findCoupon(couponSysId);
		List<CouponItem> couponItemList = storeBusiness.getCouponItemList(storeSysId, couponSysId, -1, pgno, pgsize);
		long totalCount = storeBusiness.getCouponItemCount(storeSysId, couponSysId, -1);

		model.put("coupon", coupon);
		model.put("couponItemList", couponItemList);

		int pageItemNum = couponItemList == null ? 0 : couponItemList.size();
		String otherParam = "cpsid=" + couponSysId;
		model = skinUtils
				.preparePageFooterParameters(request, pageItemNum, totalCount, pgno, pgsize, otherParam, model);

		return mv;
	}

	public ModelAndView listStoreTransaction(HttpServletRequest request, HttpServletResponse response) throws Exception {

		ModelAndView mv = new ModelAndView(storeTransactionListView);
		Map<String, Object> model = mv.getModel();

		UserSession auth = skinUtils.getUserAuthorization(request, response);
		// get site2content for this store
		Integer pgno = ServletRequestUtils.getIntParameter(request, "pgno", 0);
		Integer pgsize = ServletRequestUtils.getIntParameter(request, "pgsize", 30);

		List<StoreTransaction> storeTransactionList = storeBusiness.getStoreTransactionList(auth.getManagedStoreSysId(), pgno, pgsize);
		long totalCount = storeBusiness.getStoreTransactionCount(auth.getManagedStoreSysId());

		model.put("storeTransactionList", storeTransactionList);

		int pageItemNum = storeTransactionList == null ? 0 : storeTransactionList.size();
		String otherParam = null;
		model = skinUtils
				.preparePageFooterParameters(request, pageItemNum, totalCount, pgno, pgsize, otherParam, model);

		return mv;
	}

	public void setLogger(Logger logger) {
		this.logger = logger;
	}

	public void setProductPriceListView(String productPriceListView) {
		this.productPriceListView = productPriceListView;
	}

	public void setSetStoreServantView(String setStoreServantView) {
		this.setStoreServantView = setStoreServantView;
	}

	public void setViewStoreServantDetailView(String viewStoreServantDetailView) {
		this.viewStoreServantDetailView = viewStoreServantDetailView;
	}

	public void setProductBusiness(ProductBusiness productBusiness) {
		this.productBusiness = productBusiness;
	}

	public void setStoreBusiness(StoreBusiness storeBusiness) {
		this.storeBusiness = storeBusiness;
	}

	public void setSkinUtils(SkinUtils skinUtils) {
		this.skinUtils = skinUtils;
	}

	public void setCouponListView(String couponListView) {
		this.couponListView = couponListView;
	}

	public void setCouponItemListView(String couponItemListView) {
		this.couponItemListView = couponItemListView;
	}

	public void setCouponDao(CouponDao couponDao) {
		this.couponDao = couponDao;
	}


	public void setStoreTransactionListView(String storeTransactionListView) {
		this.storeTransactionListView = storeTransactionListView;
	}

}
